package com.baitiaojun.common.utils.string;

import com.baitiaojun.common.utils.collection.CollectionUtils;
import com.baitiaojun.common.utils.object.ObjectUtils;
import com.google.common.collect.Sets;
import org.springframework.util.AntPathMatcher;

import java.util.Arrays;
import java.util.Collections;
import java.util.List;
import java.util.Set;
import java.util.stream.Collectors;
import java.util.stream.Stream;

public class StringUtils extends org.apache.commons.lang3.StringUtils {

    /**
     * 判断字符串是否为空
     * @param str
     * @return
     */
    public static boolean isEmpty(String str) {
        return ObjectUtils.isNull(str) || EMPTY.equals(str);
    }

    public static boolean isNotEmpty(String str) {
        return !isEmpty(str);
    }

    public static String trim(String str) {
        return str == null ? "" : str.trim();
    }

    /**
     * 字符串截取
     * @param str
     * @param startPos
     * @return
     */
    public static String subString(String str, int startPos) {
        if (str == null) {
            return EMPTY;
        }
        if (startPos < 0) {
            startPos = 0;
        }
        if (startPos > str.length()) {
            return EMPTY;
        }
        return str.substring(startPos);
    }

    /**
     * @param str 要转换的字符串
     * @param separate 字符串分隔符
     * @param trim 是否去除每个字符的前后空格
     * @param filterBlank 是否过滤空字符
     * @return
     */
    public static Set<String> strToSet(String str, String separate, boolean trim, boolean filterBlank) {
        return Sets.newHashSet(strToList(str, separate, trim, filterBlank));
    }

    /**
     * @param str 要转换的字符串
     * @param separate 字符串分隔符
     * @param trim 是否去除每个字符的前后空格
     * @param filterBlank 是否过滤空字符
     * @return
     */
    public static List<String> strToList(String str, String separate, boolean trim, boolean filterBlank) {
        if (isEmpty(str)) {
            return Collections.emptyList();
        }
        Stream<String> stream = Arrays.stream(str.split(separate));
        if (trim) {
            stream = stream.filter(StringUtils::isBlank);
        }
        if (filterBlank) {
            stream = stream.map(String::trim);
        }
        return stream.collect(Collectors.toList());
    }

    /**
     * cs1中是否包含cs2中任意一个指定字符串, 忽略大小写
     * @param cs1
     * @param searchStr
     * @return
     */
    public static boolean containsAnyIgnoreCase(CharSequence cs1, CharSequence... searchStr) {
        if (isEmpty(cs1) || CollectionUtils.isEmpty(searchStr)) {
            return false;
        }
        for (CharSequence search : searchStr) {
            return containsIgnoreCase(cs1, search);
        }
        return false;
    }

    /**
     * 和所有指定路径匹配
     * @param patterns
     * @param url
     * @return
     */
    public static boolean matchAll(List<String> patterns, String url) {
        if (isEmpty(url)) {
            return false;
        }
        for (String pattern : patterns) {
            if (match(pattern, url)) {
                return true;
            }
        }
        return false;
    }

    /**
     * 利用AntPathMatcher路径匹配器匹配路径
     * @param pattern
     * @param url
     * @return
     */
    public static boolean match(String pattern, String url) {
        AntPathMatcher antPathMatcher = new AntPathMatcher();
        return antPathMatcher.match(pattern, url);
    }

    /**
     * _分割的字符串转驼峰命名
     * @param str
     * @return
     */
    public static String toCamelCase(String str) {
        if (isEmpty(str)) {
            return EMPTY;
        }
        String[] camels = str.split("_");
        if (camels.length == 0) {
            return EMPTY;
        }

        StringBuilder builder = new StringBuilder(camels.length);
        builder.append(camels[0]);
        for (int i = 1; i < camels.length; i++) {
            if (isEmpty(camels[i])) {
                continue;
            }
            String upperCase = camels[i].substring(0, 1).toUpperCase();
            String lowerCase = camels[i].substring(1).toLowerCase();
            builder.append(upperCase).append(lowerCase);
        }
        return builder.toString();
    }

    public static void main(String[] args) {
//        System.out.println(containsAnyIgnoreCase("abcd", "cd"));
        System.out.println(toCamelCase("tb_a"));
    }
}
